<template>
  <el-card class="box-card">
    <el-row :gutter="10">
      <el-col :span="10">
        <el-input placeholder="请输入搜索内容" clearable suffix-icon="el-icon-search" v-model="queryInfo.query"
          @keypress.native.enter="getOrdersList" @clear="getOrdersList"></el-input>
      </el-col>
      <el-col :span="6">
        <el-button type="primary" @click="addFormVisible = true">添加商品</el-button>
      </el-col>
    </el-row>
    <el-table style="width: 100%">
      <el-table-column label="商品信息" width="280">
      </el-table-column>
      <el-table-column label="单价" width="120">
      </el-table-column>
      <el-table-column label="数量" width="120">
      </el-table-column>
      <el-table-column label="销量实付金额" width="120">
      </el-table-column>
      <el-table-column label="交易状态" width="200">
      </el-table-column>
      <el-table-column label="操作">
      </el-table-column>
    </el-table>
    <el-table :data="tableData.ordersList" style="width: 100%">
      <el-table-column prop="id" label="ID" width="60">
      </el-table-column>
      <el-table-column width="50" prop="img">
        <template slot-scope="scope">
          <img width="40px" :src="scope.row.img" />
        </template>
      </el-table-column>
      <el-table-column prop="name" label="商品信息" width="280">
      </el-table-column>
      <el-table-column prop="price" label="价格" width="120">
      </el-table-column>
      <el-table-column prop="kc" label="库存" width="120">
      </el-table-column>
      <el-table-column prop="xl" label="销量" width="120">
      </el-table-column>
      <el-table-column prop="time" label="创建时间" width="200">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="mini" @click="showEditDialog(scope.row.id)">
          </el-button>
          <el-button type="danger" icon="el-icon-delete" circle size="mini" @click="deleteUserById(scope.row.id)">
          </el-button>
          <el-button type="text">上架</el-button>
          <!-- <el-button type="text" v-if="scope.row.status">下架</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.currentpage" :page-sizes="[2, 4]" :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next" :total="tableData.total">
      </el-pagination>
    </div>
  </el-card>
</template>

<script>
  export default {
    data() {
      return {
        queryInfo: {
          query: '',
          currentpage: 1,
          pagesize: 2
        },
        tableData: {
          ordersList: [],
          total: 0
        },
        //添加商品
        addFormVisible: false,
        editFormVisible: false,
        goodsForm: {
          id: 0,
          name: '',
          price: 0,
          kc: 0,
          xl: 0,
          time: '',
          img: ''
        },
        goodsFormRules: {
          name: [{
              required: true,
              message: '请输入商品信息',
              trigger: 'blur'
            },
            {
              min: 2,
              max: 100,
              message: '长度在 2 到 100 个字符',
              trigger: 'blur'
            }
          ]
        }
      }
    },
    created() {
      this.getOrdersList()
    },
    methods: {
      async getOrdersList() {
        const {
          data: res
        } = await this.$axios.post('/getOrdersList', this.queryInfo)
        console.log(res)
        this.tableData.ordersList = res.result
        this.tableData.total = res.total
      },
      handleSizeChange(size) {
        // console.log(size)
        this.queryInfo.pagesize = size
        this.getUserList()
      },
      handleCurrentChange(page) {
        // console.log(page)
        this.queryInfo.currentpage = page
        this.getUserList()
      },
      //添加用户
      addGoods() {
        this.$refs.addFormRef.validate(async valid => {
          if (!valid) return
          const {
            data: res
          } = await this.$axios.post('/addGoods', this.goodsForm)
          this.addFormVisible = false
          this.getOrdersList()
          this.$message.success("添加商品成功!!!")
        })
      },
      async showEditDialog(id) {
        // console.log(id)
        const {
          data: res
        } = await this.$axios.get('/getUserById', {
          data: {
            id: id
          }
        })
        this.userForm = res
        this.editFormVisible = true
      },
      editUser() {
        this.$refs.editFormRef.validate(async valid => {
          if (!valid) return
          const {
            data: res
          } = await this.$axios.post('/editUserById', this.userForm)
          this.editFormVisible = false
          this.getUserList()
          this.$message.success("修改用户成功!!!")
        })
      },
      deleteUserById(id) {
        console.log(id)
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          const {
            data: res
          } = await this.$axios.post('/deleteUserById', {
            id: id
          })
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getUserList()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      }
    }
  }
</script>

<style>
  .box-card {
    margin-top: 15px;
  }

  .block {
    margin-top: 5px;
  }
</style>
